<template>
<div class="app-container">
    <el-tabs v-model="currentTab" type="border-card" class="center-tabs"  @tab-click="handleTabClick">
      <el-tab-pane label="推广码管理" name="promoteCode" >
        <!--用户数据-->
        <el-col>
            <el-form :model="taskQueryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="任务ID" prop="taskId">
                <el-input
                  v-model="taskQueryParams.taskId"
                  placeholder="请输入任务ID"
                  clearable
                  style="width: 240px"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="创建人" prop="taskCreateUser">
                <el-input
                  v-model="taskQueryParams.taskCreateUserName"
                  placeholder="请输入创建人"
                  clearable
                  style="width: 240px"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="状态" prop="status">
                <el-select
                  v-model="taskQueryParams.taskStatus"
                  placeholder="活动状态"
                  clearable
                  style="width: 240px"
                >
                  <el-option
                    v-for="dict in dictDropDown"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="创建时间">
                <el-date-picker
                  v-model="dateRange"
                  style="width: 240px"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                ></el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb10" :inline="true">
                <el-col :span="1.5">
                  <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                  >新增分发码</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                  >导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="findTaskList" :columns="columns"></right-toolbar>
            </el-row>
            <el-table :inline="true" v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="50" align="center" />
              <el-table-column type="index" label="序列号" width="80" align="center" key="countId" v-if="columns[0].visible" />
              <el-table-column label="任务ID" align="center" key="taskId" prop="taskId" v-if="columns[1].visible" :show-overflow-tooltip="true" />
              <el-table-column label="活动标题" align="center" key="title" prop="title" v-if="columns[2].visible" :show-overflow-tooltip="true" />
              <el-table-column label="视频总数" align="center" key="videoTotalCount" prop="videoTotalCount" v-if="columns[3].visible" :show-overflow-tooltip="true" />
              <el-table-column label="创建时间" align="center" key="taskCreateTime" prop="taskCreateTime" v-if="columns[4].visible" :show-overflow-tooltip="true" />
              <el-table-column label="创建人" align="center" key="taskCreateUser" prop="taskCreateUserName" v-if="columns[5].visible"  width="120" />
              <el-table-column label="发布状态" align="center" key="taskStatus" prop="taskStatus" v-if="columns[6].visible">
                <template slot-scope="scope">
                  <el-switch
                    v-model="scope.row.taskStatus"
                    active-value="0"
                    inactive-value="1"
                    @change="handleStatusChange(scope.row)"
                  ></el-switch>
                </template>
              </el-table-column>
              <el-table-column label="推广码" align="center" >
                <template slot-scope="scope">
                  <el-popover
                  placement="bottom"
                  width="200"
                  trigger="hover">
                    <div>
                      <canvas :id="'qr-' + scope.$index" width="100" height="100"></canvas>
                    </div>
                    <span slot="reference">活动码</span>
                   <!--  <el-button slot="reference">hover 激活</el-button> -->
                  </el-popover>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                width="160"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                    <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-view"
                    @click="handleView(scope.row)">详细</el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="taskQueryParams.pageNum"
              :limit.sync="taskQueryParams.pageSize"
              @pagination="taskList"
            />
        </el-col>
      </el-tab-pane>
      <el-tab-pane label="授权信息" name ="authInfo">
        <authorEdit/>
      </el-tab-pane>
    </el-tabs>
    <!-- 弹出dailog 新增一个 二维码 -->
    <div>
        <el-dialog
            v-bind="$attrs"
            v-on="$listeners" :visible.sync="qrCreateVisible"
            @close="onClose" title="添加新推广码">
          <qrCodeCreate ref="qr-code-create" @update:visible="qrCreateVisible = false"/>
        </el-dialog>
    </div>
    <!-- 推广码 详情 -->
  <el-row :gutter="10">
    <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1">
      <div>
        <el-dialog :visible.sync="promoteCodeDetails">
          <qrCodeDetails  ref="qr-code-details"  @update:visible="promoteCodeDetails = false" :rowData="rowData"/>
        </el-dialog>
      </div>
    </el-col>
  </el-row>

</div>
</template>
<script>

import {changeTaskStatus, getTaskList} from "@/api/tuike/qrCode"
import QRCode from 'qrcode'
import authorEdit from "./authorEdit.vue"
import qrCodeDetails  from "./qrCodeDetails.vue"
import qrCodeCreate from "./qrCodeCreate"

export default {
  name: "QrCode",
  components: {authorEdit , qrCodeDetails, qrCodeCreate},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      //任务表格
      taskList: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 详细数据页
      promoteCodeDetails: false,
      // 查询参数
      taskQueryParams:{
        pageNum: 1,
        pageSize: 10,
        taskStatus: undefined,
        taskId: undefined,
        taskCreateTime: undefined,
        taskCreateUserName: undefined,
        beginTime:undefined,
        endTime:undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `序列号`, visible: true },
        { key: 1, label: `活动ID`, visible: true },
        { key: 2, label: `活动标题`, visible: true },
        { key: 3, label: `视频总数`, visible: true },
        { key: 4, label: `创建时间`, visible: true },
        { key: 5, label: `创建人`, visible: true },
        { key: 6, label: `发布状态`, visible: true },
        { key: 7, label: `推广码`, visible: true }
      ],
      currentTab: 'promoteCode',
      // dialog  数据
      qrCreateVisible: false,
      fileList:[],
      rowData:null,
      dictDropDown:[
        {
          dictLabel: "正常",
          dictValue: "0",
          dictType: "sys_normal_disable",
        },{
          dictLabel: "停用",
          dictValue: "1",
          dictType: "sys_normal_disable",
        }
      ]
    };
  },
  created() {
    this.findTaskList();
  },
  updated() {
    this.taskList.forEach((item, index) => {
      const canvas = document.getElementById('qr-' + index);
      const options = {
        width:250,
        height:250
      }
      QRCode.toCanvas(canvas, item.taskAddress,
        options,(error)=>{
        //console.log(error)
      })
    });
  },
  methods: {
    findTaskList(){
      this.loading = true;
      getTaskList(this.addDateRange(this.taskQueryParams, this.dateRange))
      .then(async response => {
          this.taskList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      )
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.taskQueryParams.deptId = data.id;
      this.handleQuery();
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.taskStatus === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + "活动" +row.taskId + '"的状态吗？')
        .then(function() {
        return changeTaskStatus(row.taskId, row.taskStatus);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.taskStatus = row.taskStatus === "0" ? "1" : "0";
      });
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        userName: undefined,
        createTime: undefined,
        phonenumber: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.taskQueryParams.pageNum = 1;
      console.log("key up....")
      this.findTaskList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/user/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.qrCreateVisible = !this.qrCreateVisible;
    },

    /**  推广详情 */
    handleView(row) {
      this.promoteCodeDetails = !this.promoteCodeDetails
      this.rowData = row;
    },

    onClose() {
      this.$refs['qr-code-create'].resetFields()
      this.findTaskList()
    },
    close() {
      this.$emit('update:visible', false)
    },

    handleTabClick(tab, event){
      console.log(tab, event);
    }
  }
};
</script>

<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>


